<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;

class Service extends Appbase 
{	
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct(){
    	if(request()->identity != 3){
      		return $this->ReError('员工账号才有权限操作！' );
    	}
		parent::__construct();
    }
	
	/**
	*服务列表
	* /appadmin/v1/service/index
	* 需要登录 
	* @is_need_login true
	*/
	public function index($name='',$cate_id='',$state=-1){
		$shopId = request()->shopId;
		$userId = request()->userId;
		try {
			$model = model('goods')->setShop($shopId)->getModel();
			$model = $model->leftJoin('shop_staff_service p','a.id = p.goods_id and p.staff_id='.$userId);
			$model = $model->field('if(p.id>0,1,0) as is_service');
			$model = $model->where('a.type',2);
			$name && $model = $model->where('a.name','like',"%".$name."%");
			$cate_id && $model = $model->where('a.cate_id',$cate_id);
			if($state==1) $model = $model->wherenotnull('p.id');
			if($state==2) $model = $model->wherenull('p.id');
			$result = $model->select();
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 回访列表
	* /appadmin/v1/service/save
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function save($ids,$is_service){
		try {  
			
			$param = [];
			foreach( array_unique(explode(",",$ids)) as $key => $val){
				$param[] = [
					'staff_id'=>request()->userId,
					'goods_id'=>$val,
				];
			}
			
			model('shop_staff_service')
					->where('staff_id',request()->userId)
					->where('goods_id','in',$ids)
					->delete();
					
			$is_service == 1 && model('shop_staff_service')->saveAll($param);
		
			return $this->ReSucess();	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 待服务
	* /appadmin/v1/service/await
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function await(){
		$identity = request()->identity;
		$shopId = request()->shopId;
		$userId = request()->userId;

		try {
			$model = model('ShopYyOrder')->alias('a')
				->leftJoin('user_vip b','b.uid = a.uid')
				->leftJoin('user_yg_proj d','d.id = a.proj_id')
				->leftJoin('goods e','e.id = d.goods_id')
				->leftJoin('goodsSpecs f','f.id = d.specs_id')
	        	->field("b.uid,b.name as user_name,b.phone")
	    		->field("a.id,a.status as state")
	    		->field("e.id as proj_id")
	    		->field("concat(e.name,' / ',f.name) as proj_name")
	    		->field("concat( DATE_FORMAT(a.date,'%Y-%m-%d'),'  ',a.times) as yy_time")
        		->order('a.add_time','desc');
        		
        	$model = $model->where('a.status','0,1');
        	$model = $model->where('a.shop_id',$shopId);
        	if($identity==3) $model = $model->where('a.staff_id',$userId);	
   
        	$data =[];
        	foreach( $model->select() as $key => $value ){
    			$value['count']  = model('ShopYyOrder')
    				->where('status',1)
    				->where('uid',$value['uid'])
    				->count();
    			$value['day']  = model('ShopYyOrder')
    				->where('status',1)
    				->where('uid',$value['uid'])
    				->field("datediff(add_time,now()) as day")
    				->limit(1)
    				->find()['day'];	
    			$value['consume'] = model('user')->consume($value['uid'],$shopId);	
    			$data[] = $value;
        	}	
        	
			//返回
			return $this->ReSucess($data);	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 待服务-确认
	* /appadmin/v1/service/confirm
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function confirm($id){
		try {
			$data = model('ShopYyOrder')->complete($id);
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 我的服务时间 - 员工专属
	* /appadmin/v1/service/times
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function times(){
		try {
			$data['shop_times'] = model('shop')->where('id',request()->shopId)->value('time');
			$data['staff_times'] = model('shop_staff')->where('id',request()->userId)->value('time');
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 我的服务时间 - 保存
	* /appadmin/v1/service/timesSave
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function timesSave($times){
		try {
			$res = model('shop_staff')->save([
				'time'=>$times
			],[
				'id'=>request()->userId
			]);
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



